/**
 ******************************************************************************
 * File Name          : stm32f4xx_hal_msp.c
 * Description        : This file provides code for the MSP Initialization
 *                      and de-Initialization codes.
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

extern DMA_HandleTypeDef hdma_usart6_rx;

extern void Error_Handler(void);

/**
 * Initializes the Global MSP.
 */
void HAL_MspInit(void)
{
    HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

    /* System interrupt init*/
    /* MemoryManagement_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
    /* BusFault_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
    /* UsageFault_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
    /* SVCall_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
    /* DebugMonitor_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
    /* PendSV_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
}

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    if (hadc->Instance == ADC1)
    {
        __HAL_RCC_ADC1_CLK_ENABLE()
        ;

        GPIO_InitStruct.Pin = GPIO_PIN_4;
        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
    if (hadc->Instance == ADC1)
    {
        __HAL_RCC_ADC1_CLK_DISABLE();
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4);

    }
}

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    if (hspi->Instance == SPI1)
    {
        __HAL_RCC_SPI1_CLK_ENABLE()
        ;

        /**SPI1 GPIO Configuration
         PA5     ------> SPI1_SCK
         PA6     ------> SPI1_MISO
         PA7     ------> SPI1_MOSI
         */
        GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
    else if (hspi->Instance == SPI2)
    {
        __HAL_RCC_SPI2_CLK_ENABLE()
        ;

        /**SPI2 GPIO Configuration
         PB13     ------> SPI2_SCK
         PB14     ------> SPI2_MISO
         PB15     ------> SPI2_MOSI
         */
        GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{

    if (hspi->Instance == SPI1)
    {
        __HAL_RCC_SPI1_CLK_DISABLE();

        /**SPI1 GPIO Configuration
         PA5     ------> SPI1_SCK
         PA6     ------> SPI1_MISO
         PA7     ------> SPI1_MOSI
         */
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
    }
    else if (hspi->Instance == SPI2)
    {
        __HAL_RCC_SPI2_CLK_DISABLE();

        /**SPI2 GPIO Configuration
         PB13     ------> SPI2_SCK
         PB14     ------> SPI2_MISO
         PB15     ------> SPI2_MOSI
         */
        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
    }
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
	GPIO_InitTypeDef GPIO_InitStruct;
    if (htim_base->Instance == TIM1)
    {
        __HAL_RCC_TIM1_CLK_ENABLE()
        ;
    }
    else if (htim_base->Instance == TIM3)
    {
    	    __HAL_RCC_TIM3_CLK_ENABLE();
    	    /**TIM3 GPIO Configuration
    	    PB0     ------> TIM3_CH3
    	    PB1     ------> TIM3_CH4
    	    PB4     ------> TIM3_CH1
    	    PB5     ------> TIM3_CH2
    	    */
    	    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
    	    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    	    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    	    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    	    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
    	    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    	    HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
    	    HAL_NVIC_EnableIRQ(TIM3_IRQn);
    }
    else if (htim_base->Instance == TIM4)
    {
    	    __HAL_RCC_TIM4_CLK_ENABLE();

    	    /**TIM4 GPIO Configuration
    	    PD12     ------> TIM4_CH1
    	    PD13     ------> TIM4_CH2
    	    PD14     ------> TIM4_CH3
    	    PD15     ------> TIM4_CH4
    	    */
    	    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
    	    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    	    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    	    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    	    GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
    	    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    	    HAL_NVIC_SetPriority(TIM4_IRQn, 3, 0);
    	    HAL_NVIC_EnableIRQ(TIM4_IRQn);
    }
    else if (htim_base->Instance == TIM5)
    {
        __HAL_RCC_TIM5_CLK_ENABLE()
        ;
    }
    else if (htim_base->Instance == TIM2)
    {
        __HAL_RCC_TIM2_CLK_ENABLE()
        ;
    }

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    if (htim->Instance == TIM1)
    {
        /**TIM1 GPIO Configuration
         PA8     ------> TIM1_CH1
         PA9     ------> TIM1_CH2
         PE13     ------> TIM1_CH3
         PE14     ------> TIM1_CH4
         */
        GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
    }
    else if (htim->Instance == TIM5)
    {
        /**TIM5 GPIO Configuration
         PA0-WKUP     ------> TIM5_CH1
         PA1     ------> TIM5_CH2
         PA2     ------> TIM5_CH3
         PA3     ------> TIM5_CH4
         */
        GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
}


void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{

    if (htim_base->Instance == TIM1)
    {
        __HAL_RCC_TIM1_CLK_DISABLE();
    }
    else if (htim_base->Instance == TIM5)
    {
        __HAL_RCC_TIM5_CLK_DISABLE();
    }
}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    if (huart->Instance == UART4)
    {
        __HAL_RCC_UART4_CLK_ENABLE()
        ;

        /**UART4 GPIO Configuration
         PC10     ------> UART4_TX
         PC11     ------> UART4_RX
         */
        GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

        /* Peripheral interrupt init */
        HAL_NVIC_SetPriority(UART4_IRQn, 5, 0);
        HAL_NVIC_EnableIRQ(UART4_IRQn);
    }
    else if (huart->Instance == UART5)
    {
        __HAL_RCC_UART5_CLK_ENABLE()
        ;

        /**UART5 GPIO Configuration
         PD2     ------> UART5_RX
         */

        GPIO_InitStruct.Pin = GPIO_PIN_2;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
        HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

        /* Peripheral interrupt init */
        HAL_NVIC_SetPriority(UART5_IRQn, 5, 0);
        HAL_NVIC_EnableIRQ(UART5_IRQn);
    }
    else if (huart->Instance == USART1)
    {
        __HAL_RCC_USART1_CLK_ENABLE()
        ;

        /**USART1 GPIO Configuration
         PB6     ------> USART1_TX
         PB7     ------> USART1_RX
         */
        GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if (huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE()
        ;

        /**USART2 GPIO Configuration
         PD5     ------> USART2_TX
         PD6     ------> USART2_RX
         */
        GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
        HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

        /* Peripheral interrupt init */
        HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
        HAL_NVIC_EnableIRQ(USART2_IRQn);
    }
    else if (huart->Instance == USART3)
    {
        __HAL_RCC_USART3_CLK_ENABLE()
        ;

        /**USART3 GPIO Configuration
         PB10     ------> USART3_TX
         PB11     ------> USART3_RX
         */
        GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if (huart->Instance == USART6)
    {
        __HAL_RCC_USART6_CLK_ENABLE()
        ;

        /**USART6 GPIO Configuration
         PC6     ------> USART6_TX
         PC7     ------> USART6_RX
         */
        GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

        /* Peripheral DMA init*/
        hdma_usart6_rx.Instance = DMA2_Stream2;
        hdma_usart6_rx.Init.Channel = DMA_CHANNEL_5;
        hdma_usart6_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_usart6_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_usart6_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_usart6_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_usart6_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_usart6_rx.Init.Mode = DMA_NORMAL;
        hdma_usart6_rx.Init.Priority = DMA_PRIORITY_LOW;
        hdma_usart6_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_usart6_rx) != HAL_OK)
        {
            Error_Handler();
        }

        __HAL_LINKDMA(huart, hdmarx, hdma_usart6_rx);

        /* Peripheral interrupt init */
        HAL_NVIC_SetPriority(USART6_IRQn, 5, 0);
        HAL_NVIC_EnableIRQ(USART6_IRQn);
    }

}

void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{

    if (huart->Instance == UART4)
    {
        __HAL_RCC_UART4_CLK_DISABLE();

        /**UART4 GPIO Configuration
         PC10     ------> UART4_TX
         PC11     ------> UART4_RX
         */
        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11);

        /* Peripheral interrupt DeInit*/
        HAL_NVIC_DisableIRQ(UART4_IRQn);
    }
    else if (huart->Instance == UART5)
    {
        __HAL_RCC_UART5_CLK_DISABLE();

        /**UART5 GPIO Configuration
         PC12     ------> UART5_TX
         PD2     ------> UART5_RX
         */
        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);

        HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);

        /* Peripheral DMA DeInit*/
        HAL_DMA_DeInit(huart->hdmarx);

        /* Peripheral interrupt DeInit*/
        HAL_NVIC_DisableIRQ(UART5_IRQn);
    }
    else if (huart->Instance == USART1)
    {
        __HAL_RCC_USART1_CLK_DISABLE();

        /**USART1 GPIO Configuration
         PB6     ------> USART1_TX
         PB7     ------> USART1_RX
         */
        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6 | GPIO_PIN_7);
    }
    else if (huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_DISABLE();

        /**USART2 GPIO Configuration
         PD5     ------> USART2_TX
         PD6     ------> USART2_RX
         */
        HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5 | GPIO_PIN_6);

        /* Peripheral interrupt DeInit*/
        HAL_NVIC_DisableIRQ(USART2_IRQn);
    }
    else if (huart->Instance == USART3)
    {
        __HAL_RCC_USART3_CLK_DISABLE();

        /**USART3 GPIO Configuration
         PB10     ------> USART3_TX
         PB11     ------> USART3_RX
         */
        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
    }
    else if (huart->Instance == USART6)
    {
        __HAL_RCC_USART6_CLK_DISABLE();

        /**USART6 GPIO Configuration
         PC6     ------> USART6_TX
         PC7     ------> USART6_RX
         */
        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6 | GPIO_PIN_7);

        /* Peripheral interrupt DeInit*/
        HAL_NVIC_DisableIRQ(USART6_IRQn);
    }

}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
